package plenprojects.chatroomcontrollers;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import plenprojects.chatservice.ChatRoom;
import plenprojects.chatservice.ChatService;
import plenprojects.model.ServerError;
import plenprojects.model.ServerMessage;

@Controller
@RequestMapping("/info")
public class InfoController {
	
	@Autowired
	private ChatService service;
	
	@RequestMapping(value = "/chatrooms", method = RequestMethod.GET)
	public @ResponseBody ServerMessage getAllChatNames() {
		
		ServerMessage response = new ServerMessage();
		
		List<String> roomNames = service.getListOfChatRooms();
		
		response.setParameter("rooms", roomNames);
		
		return response;
	}
	
	@RequestMapping(value = "/chatroom/{roomname}", method = RequestMethod.GET)
	public @ResponseBody ServerMessage getUsersInChat(@PathVariable String roomname) {
		
		ServerMessage response = new ServerMessage();
		
		ChatRoom room = service.getRoom(roomname);
		
		if(room == null) {
			
			return new ServerError("Chat room " + roomname + " doesn't exist.");
		}
		
		List<String> users = new ArrayList<String>(room.getUsersInRoom());
		
		response.setParameter("success", true);
		response.setParameter("users", users);
		return response;
	}
}
